package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.TerTerminalDueDao;
import com.framework.entity.ter.TerTerminalDue;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.exception.RRException;
import com.framework.page.PageUtils;
import com.framework.service.TerTerminalDueService;
import com.framework.utils.string.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Service("terTerminalDueService")
public class TerTerminalDueServiceImpl extends ServiceImpl<TerTerminalDueDao,TerTerminalDue> implements TerTerminalDueService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addTerminalDueList(List<TerTerminalInfo> tempList, Long isOnline, BigDecimal reapAmount, Date dueDate, BigDecimal afterCash, Long deptId) {
        //考拉的渠道id，非考拉的不添加
        if(!deptId.equals(889326L)){
            return;
        }
        //线上才添加
        if(isOnline.intValue()==1){

               if(dueDate==null){
                   throw new RRException("9000","请选择返现期限");
               }

               if(afterCash==null){
                   throw new RRException("9000","请输入返现金额");
               }

            TerTerminalDue due;
            for (TerTerminalInfo terminalInfo :tempList) {
                due = new TerTerminalDue();
                due.setAfterCash(afterCash);
                due.setCreateTime(new Date());
                due.setUpdateTime(new Date());
                due.setCurrentCash(reapAmount);
                due.setSn(terminalInfo.getSn());
                due.setDueDate(dueDate);
                due.setDeptId(deptId);
                baseMapper.insert(due);

            }

        }
    }


    @Override
    public PageUtils queryDuePageByMap(Map<String, Object> params) {
        if(params.get("startSn")!=null && params.get("endSn")!=null){
            if(StringUtil.isNumeric(params.get("startSn").toString()) && StringUtil.isNumeric(params.get("endSn").toString()) ){
                params.put("numSign","Yes");
            }else{
                params.put("numSign","No");
            }
        }
        Integer currPage = Integer.valueOf(params.get("page").toString());
        Integer pageSize = Integer.valueOf(params.get("limit").toString());
        params.put("start", (currPage - 1) * pageSize);
        params.put("limit", pageSize);
        List<Map<String, Object>> list = baseMapper.queryTerminalListByMap(params);
        Integer totalCount = baseMapper.queryTerminalCountByMap(params);
        return new PageUtils(list,totalCount,pageSize,currPage);
    }

    @Override
    public Map<String, Object> getInfoByIdList(Integer[] idList) {
        Map<String,Object> result=new HashMap<>();
        result.put("idList",idList);
        List<TerTerminalDue> list=baseMapper.selectbyIdList(idList);
        StringBuilder sb=new StringBuilder();
        for (TerTerminalDue terTerminalDue : list) {
            sb.append(terTerminalDue.getSn()+",");
        }
        String snStr=sb.substring(0,sb.lastIndexOf(","));
        result.put("snStr",snStr);
        String currentCash="";
        for (TerTerminalDue terTerminalDue :list) {
            if(!currentCash.contains(terTerminalDue.getCurrentCash().toEngineeringString())){
                currentCash+=","+terTerminalDue.getCurrentCash().toEngineeringString();
            }
        }
        result.put("currentCash",currentCash);
        return result;
    }

    @Override
    public void updateByIdList(Map<String, Object> activDue) {
        baseMapper.updateTerminalDueByMap(activDue);
    }
}
